package Entities;

import java.util.ArrayList;
import Entities.Purchase_order;
import digitshoplogic.DBAccessSingleton;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class Terminal {
	public int terminalId;
	public int storeId;
	
    public Terminal( ) {
    }

    public static final Terminal getTerminalByID(int id) throws SQLException {
        Terminal t = new Terminal();

        PreparedStatement  ps;
         Connection con = DBAccessSingleton.getInstance().getConnection();
         ps = con.prepareStatement("select * from Terminal where terminal.terminalId = ?");
         ps.setInt(1, id);

        ResultSet rs = ps.executeQuery();

         while (rs.next()) {
            t.terminalId = rs.getInt(1);
            t.storeId = rs.getInt(2);
        }
        rs.close();
        return t;
        }
     public static final void addTerminalByObj(Terminal t) throws SQLException {

         PreparedStatement ps;
         Connection con = DBAccessSingleton.getInstance().getConnection();
         ps = con.prepareStatement("INSERT INTO terminal VALUES (?,?)");

         ps.setInt(1, t.terminalId);
         ps.setInt(2, t.storeId);

         ps.executeUpdate();
         ps.close();
        }

 public static final void deleteTerminalByID(int id) throws SQLException{

         PreparedStatement  ps;
         Connection con = DBAccessSingleton.getInstance().getConnection();
         ps = con.prepareStatement("DELETE FROM terminal WHERE terminal.terminalId= ?");
	 ps.setInt(1, id);
         ps.executeUpdate();
         ps.close();
     }
  public static final void updateTerminalByObj(Terminal t) throws SQLException{

         PreparedStatement  ps;
         Connection con = DBAccessSingleton.getInstance().getConnection();
         ps = con.prepareStatement("UPDATE terminal SET "
                 + "terminal.storeId = ? WHERE terminal.terminalId = ?");

	 ps.setInt(2, t.terminalId);
         ps.setInt(1, t.storeId);

         ps.executeUpdate();
         ps.close();

     } 
   

    public int getTerminalId() {
        return terminalId;
    }

    public void setTerminalId(int terminalId) {
        this.terminalId = terminalId;
    }

}